package cn.weakup.designpattern.a_foreword.calculator01;

import java.util.Scanner;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/2
 * @desc 实现控制台数据两个数，实现加减乘除运算
 */
public class Calculator {

    public static void main(String[] args) {
        String flag;
        do {
            System.out.println("请输入第一个数字");
            Scanner scanner = new Scanner(System.in);
            String numberA = scanner.nextLine();
            System.out.println("请输入第二个数字");
            String numberB = scanner.nextLine();
            System.out.println("请输入运算符");
            String option = scanner.nextLine();

            String result = handleProgram(numberA, numberB, option);

            System.out.println("结果为：" + result);
            System.out.println("继续请按1，退出请按0");
            flag = scanner.nextLine();
        } while ("1".equals(flag));

    }

    /**
     * 处理计算逻辑
     * @param numberA
     * @param numberB
     * @param option
     * @return
     */
    private static String handleProgram(String numberA, String numberB, String option) {
        int result = 0;
        try {
            Integer numA = Integer.valueOf(numberA);
            Integer numB = Integer.valueOf(numberB);
            switch (option) {
                case "+":
                    result = numA + numB;
                    break;
                case "-":
                    result = numA - numB;
                    break;
                case "*":
                    result = numA * numB;
                    break;
                case "/":
                    result = numA / numB;
                    break;
            }
        } catch (Exception e) {
            return e.getMessage();
        }
        return String.valueOf(result);
    }
}
